package com.jiutianniao.p2p.acctrans.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jiutianniao.p2p.acctrans.core.dal.dao.AccountDao;
import com.jiutianniao.p2p.acctrans.core.dal.model.AccountDO;
import com.jiutianniao.p2p.acctrans.core.service.AccountService;
import com.jiutianniao.p2p.base.model.Sequence.SequenceName;
import com.jiutianniao.p2p.base.service.SequenceService;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Resource
	private AccountDao accountDao;

	@Resource
	private SequenceService sequnceService;

	@Override
	public AccountDO selectForLock(String accountNo) {
		return accountDao.selectForUpdateByAccountNo(accountNo);
	}

	@Override
	public boolean updateForBalanceAndFreezeAmount(AccountDO accountDO) {
		return 1 == accountDao.updateBalanceAndFreeze(accountDO);
	}

	@Override
	public boolean updateBalence(AccountDO accountDO) {
		return 1 == accountDao.updateBalance(accountDO);
	}

	@Override
	public boolean updateFreeze(AccountDO accountDO) {
		return 1 == accountDao.updateFreeze(accountDO);
	}

	@Override
	public void creatNewAccount(AccountDO accountDO) {
		accountDao.create(accountDO);
	}

	@Override
	public AccountDO getAccountDO(String accountNo) {
		return accountDao.getByAccountNo(accountNo);
	}

	@Override
	public String getAccountNextSequenceStr() {
		long nextSequence = sequnceService
				.getNextBySequenceName(SequenceName.ACCOUNT_SEQUENCE.name());
		String str = String.format("%010d", nextSequence);
		return str;
	}

	@Override
	public List<AccountDO> queryByNoList(List<String> accountNoList) {
		return accountDao.queryByNoList(accountNoList);
	}

}
